<template>
  <el-card body-style="min-height: 86vh">
    <!-- 搜索 -->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="检查项名称">
        <el-input v-model="searchForm.precheckName" placeholder="请输入检查项名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleRestSearch()">重置</el-button>
        <el-button type="primary" @click="handleGetTableList()">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-divider />
    <!-- 新增、筛选按钮 -->
    <div class="flex justify-between">
      <div>
        <el-button type="primary" @click="handleCreate">新增内饰检查项</el-button>
      </div>
      <div>
        <el-button circle @click="handleGetTableList()">
          <template #icon
            ><el-icon> <Refresh /> </el-icon
          ></template>
        </el-button>
      </div>
    </div>
    <!-- 表格 -->
    <el-table :data="tableData">
      <el-table-column label="检查项名称" prop="precheckName" align="center"></el-table-column>
      <el-table-column label="备注" prop="remark" align="center"></el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template #default="{ row }">
          <el-tag :type="row.status === 1 ? 'primary' : 'danger'">{{
            row.status === 1 ? "启用" : "禁用"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="primary" @click="handleEdit(row)">编辑</el-button>
          <el-popconfirm
            title="你确定要删除该检测项?"
            width="200"
            @confirm="handleDelete({ id: row.id })"
          >
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="flex justify-center mt-2"
      background
      layout="total,prev, pager, next"
      :current-page="currentPage"
      :page-size="limit"
      :total="total"
      @current-change="handleGetTableList"
    />
    <form-drawer ref="formDrawerRef" @submit="handleSubmit()">
      <el-form ref="formRef" :model="form" label-width="120" :rules="rules">
        <el-form-item label="检查项名称" prop="precheckName">
          <el-input v-model="form.precheckName" placeholder="请输入检查项名称"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入检查项名称"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            inline-prompt
            active-text="启用"
            inactive-text="禁用"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
    </form-drawer>
  </el-card>
</template>
<script setup lang="ts">
import { useInitTable, useInitForm } from "@/hooks/useCommon";
import FormDrawer from "@/components/FormDrawer.vue";
import { getExaminePageApi, addExamineApi, editExamineApi, deleteExamineApi } from "@/api/examine";

// 表格
const { tableData, searchForm,currentPage, limit, total, handleRestSearch, handleGetTableList, handleDelete } =
  useInitTable({
    searchForm: {
      type: 2,
      precheckName: ""
    },
    getTableList: getExaminePageApi,
    deleteData: deleteExamineApi
  });
// 新增编辑
const { formDrawerRef, formRef, form, rules, handleCreate, handleEdit, handleSubmit } = useInitForm(
  {
    form: {
      type: 2,
      precheckName: "",
      remark: "",
      status: 1
    },
    rules: {
      precheckName: [
        {
          required: true,
          message: "请输入检测项名称",
          trigger: "blur"
        }
      ]
    },
    createData: addExamineApi,
    updateData: editExamineApi,
    success: () => {
      handleGetTableList(currentPage.value);
    }
  }
);
</script>
<style scoped lang="scss"></style>
